<template>
  <view>
    <!-- 搜索框 -->
    <view class="search-box">
      <uni-search-bar  @input="input" cancelButton="none" :radius='100' focus='true'></uni-search-bar>
    </view>
    
    <!-- 搜索建议列表 -->
    <view class="sugg-list" v-if="searchResults.length !== 0">
      <view class="sugg-item" v-for="(item, i) in searchResults" :key='i' @click="gotoDetail(item)">
        <view class="good-name">{{item.goods_name}}</view>
        <uni-icons type="right" size="16" ></uni-icons>
      </view>
    </view>
    
    <!-- 搜索历史区域 -->
    <view class="history-box" v-else>
      <!-- 历史标题 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="cleanHistoryList"></uni-icons>
      </view>
      <!-- 历史列表 -->
      <view class="history-list">
        <uni-tag :text="item" inverted='true' v-for="(item, i) in histories" :key='i' style="margin-top: 5px; margin-right: 5px;" @click="gotoGoodList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        //用于延时器防抖处理
        timer:null,
        // 搜索关键词
        kw:'',
        // 搜索建议列表
        searchResults:[],
        // 搜索历史记录
        historyList:[]
      };
    },
    onLoad() {
      // 调用方法 初始化时候加载本地存储的搜索历史记录
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    },
    methods:{
      input(e){
        // 为输入框加入防抖处理
        // 清除对应的延时器
        clearTimeout(this.timer)
        //重新启动一个延时器并把timerId赋值给this.timer
        this.timer = setTimeout(()=>{
          // 如果500毫秒没有更改搜索框的内容那么那么将内容赋值给搜索关键词
          this.kw = e,
          this.getSearchList()
          
        }, 500)
      },
      // 获取搜索列表
      async getSearchList(){
        // 判断请求是个否为空
        if(this.kw === ''){
          this.searchResults = [];
          return 
        }
        const {data: res} = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.kw})
        
        if(res.meta.status !== 200) return uni.$showMsg()
        this.searchResults = res.message
        this.saveSearchHistory()
      },
      // 保存搜索关键词为历史记录
      saveSearchHistory(){
        const set = new Set(this.historyList)
        set.delete(this.kw)
        set.add(this.kw)
        this.historyList = Array.from(set)
        // 调用方法将搜索历史存储到本地
        uni.setStorageSync('kw', JSON.stringify(this.historyList))
        
      },
      // 清空历史记录
      cleanHistoryList(){
        this.historyList = [],
        uni.clearStorageSync('kw')
      },
      // 搜索历史标签tag的点击跳转事件
      gotoGoodList(kw){
        uni.navigateTo({
          url:'/subpkg/good_list/good_list?query=' + kw
        })
      },
      gotoDetail(item){
        uni.navigateTo({
          url:'/subpkg/good_detail/good_detail?goods_id=' + item.goods_id
        })
      }
    },
    computed:{
      histories(){
        return [...this.historyList].reverse()
      }
    }
  }
</script>

<style lang="scss">
.search-box{
  background-color: #c00000;
  // 吸顶效果
  position: sticky;
  top: 0;
  z-index: 999;
}
.sugg-list{
  padding: 0 5px;
  .sugg-item{
    font-size: 12px;
    display: flex;
    border-bottom: 1px solid #efefef;
    align-items: center;
    justify-content: space-between;
    padding: 13px 0;
    .good-name{
      //文字不允许换行 单行文本
      white-space: nowrap;
      //溢出部分进行隐藏
      overflow: hidden;
      //用...代替溢出部分
      text-overflow: ellipsis;
      margin-right: 3px;
    }
  }
}

.history-box{
  padding: 0 5px;
  
  .history-title{
    display: flex;
    font-size: 13px;
    height: 40px;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #efefef;
  }
  .history-list{
    display: flex;
    flex-wrap: wrap; 
    
    
  }
}

</style>
